﻿using Microsoft.AspNetCore.Http;
using System;
using Aierp.SystemEntities.System;

namespace Aierp.Utility.Core
{
    public class SysVisitor
    {
        #region Session Key

        public const string SessionUserIdKey = "AIERP-CUP-ADMIN-USERID";
        public const string SessionUserNameKey = "AIERP-CUP-ADMIN-USERNAME";
        public const string SessionIsAdminKey = "AIERP-CUP-ADMIN-ISADMIN";
        /// <summary>
        /// 用户可访问的部门列表
        /// </summary>
        public const string SessionDepartmentsKey = "AIERP-CUP-USER-DEPARTMENTS";
        #endregion

        #region CookieName Key

        public const string CookieNameKey = "AIERP-CUP-COOKIE-NAME";
        public const string CookieUserNameKey = "AIERP-CUP-COOKIE-USERNAME";
        public const string CookiePasswordKey = "AIERP-CUP-COOKIE-PASSWORD";
        public const string CookieDepartmentsKey = "AIERP-CUP-COOKIE-USER-DEPARTMENTS";
        #endregion

        private readonly IHttpContextAccessor _httpContextAccessor;
        public ISession Session => _httpContextAccessor.HttpContext.Session;
        public SysVisitor(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId
        {
            get {return (Session.GetInt32(SessionUserIdKey))??0;}
            set { Session.SetInt32(SessionUserIdKey, value); }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get
            {
                return (Session.GetString(SessionUserNameKey));
            }
            set
            {
                Session.SetString(SessionUserNameKey, value);
            }
        }

        /// <summary>
        /// 是否超级管理员
        /// </summary>
        public bool IsAdmin
        {
            get { return Convert.ToBoolean(Session.GetString(SessionIsAdminKey)); }
            set { Session.SetString(SessionIsAdminKey, value.ToString()); }
        }

        /// <summary>
        /// 当前用户
        /// </summary>
        public Sys_Users CurrentUser
        {
            get { return JSONhelper.ConvertToObject<Sys_Users>(Session.GetString("AIERP-CUP-ADMIN")); }
            set { Session.SetString("AIERP-CUP-ADMIN", JSONhelper.ToJson(value)); }
        }

        /// <summary>
        /// 当前用户可以访问的部门数据
        /// </summary>
        public string Departments
        {
            get { return (Session.GetString(SessionDepartmentsKey)); }
            set { Session.SetString(SessionDepartmentsKey, value); }
        }
    }
}
